package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类与品牌管理")
@RestController
@RequestMapping("/admin/product/baseCategoryTrademark")
public class BaseCategoryTradeMarkController {

    @Autowired
    private BaseCategoryTradeMarkService baseCategoryTradeMarkService;

    /**
     * 根据三级分类Id 查询品牌列表
     * @param category3Id
     * @return
     */
    @GetMapping("/findTrademarkList/{category3Id}")
    public Result findTrademarkList(@PathVariable Long category3Id){
        //  服务层方法.
        List<BaseTrademark>  baseTrademarkList = this.baseCategoryTradeMarkService.getTrademarkList(category3Id);

        //  返回数据
        return Result.ok(baseTrademarkList);
    }

    /**
     * 根据三级分类Id 获取到可选品牌列表。
     * @param category3Id
     * @return
     */
    @GetMapping("/findCurrentTrademarkList/{category3Id}")
    public Result findCurrentTrademarkList (@PathVariable Long category3Id){
        //  服务层方法.
        List<BaseTrademark>  baseTrademarkList = this.baseCategoryTradeMarkService.getCurrentTrademarkList(category3Id);
        //  返回数据
        return Result.ok(baseTrademarkList);
    }

    /**
     * 保存分类Id 与 品牌的关系
     * @param categoryTrademarkVo
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody CategoryTrademarkVo categoryTrademarkVo){
        //  调用服务层方法.
        this.baseCategoryTradeMarkService.save(categoryTrademarkVo);
        return Result.ok();
    }

    /**
     * 删除品牌与分类的关系
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @DeleteMapping("/remove/{category3Id}/{trademarkId}")
    public Result remove(@PathVariable Long category3Id,
                         @PathVariable Long trademarkId) {
        //  调用服务层方法.
        //  属于逻辑删除。
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        this.baseCategoryTradeMarkService.remove(trademarkLambdaQueryWrapper);
        //  使用了方法重载删除数据.
        //  this.baseCategoryTradeMarkService.remove(category3Id,trademarkId);
        return Result.ok();
    }
}
